/**
 * video -> music 首页
 * @author tanggf
 */
$(function(){
	music_index.run();
});

var music_index = {
		run: function(){
			this._init_();
			this._start_();			
			this._events_();
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val();
			this.vdInstalled = vd.check();
		},
		
		_start_: function(){
			vevo_top.run();
			yb_top.run();
			
			var ua = navigator.userAgent;			
			if(ua.indexOf('Mac') >= 0){
				this.$alertDialog = $("#donwolad-alert-mac");
			}else{
				this.$alertDialog = $("#donwolad-alert");
			}
			
			if( ! this.vdInstalled){
				music_index.$alertDialog.dialog({
					submit: function(){
						vd.keep();
						_gaq.push(['_trackEvent', 'TVShow', 'Download', music_index.itemUrl]);
						vd.go(music_index.itemUrl, music_index.title);		
						music_index.vdInstalled = true;
					}
				});	
			}			
		},
		
		_events_: function(){
			
			$("#popular_wrap_div > div > div.slides-control > ul > li > div a").click(function(){
				var reg = /^.+v=(.+)&.+$|^.+v=(.+)$/g;
				var url = $(this).attr("href");							
				if(reg.test(url)){
					var v = RegExp.$1 || RegExp.$2;
					location.href = music_index.contextPath + "/video/play/yt/yt" + v;					
					return false;
				}
			});
		}
};

var vevo_top = {
		run: function(){
			//switch tab
			$("#music_box_div").tabs();
			//vevo top artist slides
			$("#top_artist_daily_div").slides({
				onslide: function(page, $dd, $this){					
					if(page > 1){
						vevo_top._handle_vevo_top_slide(page, $dd, $this, 'artist');
					}					
				}
			});
			
			//vevo top video slides
			$("#top_video_daily_div").slides({
				onslide: function(page, $dd, $this){				
					if(page > 1){
						vevo_top._handle_vevo_top_slide(page, $dd, $this, 'video');
					}					
				},
				slideWidth: 605
			});
			
			//vevo video, artist daily switch
			$("#vevo_video_div, #vevo_artist_div").tabs({
				onswitch: function($nav, $tab, $this){
					vevo_top._handle_vevo_period_tabs($nav, $tab, $this);
				},
				hoverClass: 'sort-by-time-now'
			});
			
			//播放,favorite事件
			var $artistLi = $("#top_artist_daily_div > div.slides-control > ul > li");
			vevo_top._handle_play_favorite_click($artistLi, 'artist');			
			
			var $videoLi = $("#top_video_daily_div > div.slides-control > ul > li");
			vevo_top._handle_play_favorite_click($videoLi, 'video');			
		},	
		
		//绑定播放,收藏事件
		_handle_play_favorite_click: function($li, type){
			//alert($li.children("div.music-slide-list-button").children("div.ytb-button-content").html());
			$li.children("div.music-slide-list-button").children("div.ytb-button-content").children("span.ytb-button-a").click(function(){
				var title = $(this).attr("name");
				var url = $(this).attr("url");
				_gaq.push(['_trackEvent', 'Vevo_' + type, 'Play', title]);	
				//window.open(url,'_target');	
				location.href = url;
			});

			$li.children("div.music-slide-list-button").children("div.ytb-button-content").children("span.ytb-button-b").click(function(){
				var title = $(this).attr("name");
				var url = $(this).attr("url");
				if( ! music_index.vdInstalled){
					music_index.itemUrl = url;
					music_index.title = title;
					music_index.$alertDialog.dialog('open');					
				}else{
					_gaq.push(['_trackEvent', 'Vevo_' + type, 'Download', url]);
					vd.go(url, title);					
				}			
			});
			
			/*
			$("div.music-slide-list-button > span.music-add-ico", $li).click(function(){
				var title = $(this).attr("name");
				_gaq.push(['_trackEvent', 'Vevo_' + type, 'Favorite', title]);	
			});			
			*/
		},
		
		_handle_vevo_top_slide: function(page, $dd, $ele, type){			
			var loaded = $dd.data("loaded");
			if( ! loaded){				
				var period = $ele.attr("period");
				var url = [music_index.contextPath, "/video/music/vevo_", type, "_list?period=", period, "&page=", page].join("");
				$.get(url, function(result){
					$dd.data("loaded", true);	
					var $li = $dd.html(result).find("li");
					vevo_top._handle_play_favorite_click($li, type);					
				});
			}
		},
		
		_handle_vevo_period_tabs: function($nav, $tab, $this){
			var period = $nav.attr("period");
			if(period){
				var loaded = $nav.data("loaded");
				if( ! loaded){
					var type = $this.attr("type");
					var url = [music_index.contextPath, '/video/music/vevo/', type, '/', period].join("");
					$.get(url, function(result){
						$nav.data("loaded", true);
						//bind slide event
						$("#top_" + type + "_" + period + "_div").html(result).slides({
							onslide: function(page, $dd, $ele){
								if(page > 1){
									vevo_top._handle_vevo_top_slide(page, $dd, $ele, type);
								}								
							}
						});				
						
						var $li = $("div.slides-control > ul > li", $tab);
						//绑定播放,收藏事件
						vevo_top._handle_play_favorite_click($li, type);
					});
				}				
			}			
		}
};

var yb_top = {
		run: function(){
			//vmenu
			$("#popular_select_div").vmenu({
				onchange: function(value){
					$("#popular_wrap_div > div").hide().eq(value - 1).show();
				}
			});
			//slides				
			//top inside slides
			$("#billboard_slide_div").slides();
			
			$("#yb_weekly_slide_div, #yb_daily_slide_div").slides({
				onslide: function(index, $ul, $this){
					yb_top._handle_yb_slide(index, $ul, $this);
				}				
			});	
			
			//play 事件
			var $ybwli = $("#yb_weekly_slide_div > div.slides-control > ul > li");
			yb_top._handle_play_click($ybwli, 'youtube_weekly');
			
			var $ybdli = $("#yb_daily_slide_div > div.slides-control > ul > li");
			yb_top._handle_play_click($ybdli, 'youtube_daily');
			
			var $bbli = $("#billboard_slide_div > div.slides-control > ul > li");
			yb_top._handle_play_click($bbli, 'billboard');
		},
		
		_handle_yb_slide: function(page, $ul, $ele){
			var loaded = $ul.data("loaded");			
			if( ! loaded){
				var period = $ele.attr("period");
				var url = [music_index.contextPath, "/video/music/yb_list?period=", period, "&page=", page].join("");
				$.get(url, function(result){
					$ul.data("loaded", true);
					var $li = $ul.html(result).find("li");
					yb_top._handle_play_click($li, 'youtube_' + period);
				});
			}
		},
		
		_handle_play_click: function($li, type){
			$("div > a, h3 > a", $li).click(function(){
				var title = $(this).attr("name");
				_gaq.push(['_trackEvent', type, 'Play', title]);	
			});		
		}
};